package com.xx.util;

import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.internal.FormatStyle;
import org.hibernate.engine.jdbc.internal.Formatter;

import java.io.FileOutputStream;
import java.io.IOException;

public class HibernateHelper {

    private Configuration config = null;

    public HibernateHelper() {
        config = new Configuration();
    }

    public HibernateHelper setDialect(String dialect) {
        config.setProperty(AvailableSettings.DIALECT, dialect);
        return this;
    }

    /**
     * Method determines classes which will be used for DDL generation.
     *
     * @param annotatedClasses - entities annotated with Hibernate annotations.
     */
    public HibernateHelper addAnnotatedClasses(Class[] annotatedClasses) {
        for (Class clazz : annotatedClasses) {
            config.addAnnotatedClass(clazz);
        }
        return this;
    }

    /**
     * Method performs translation of entities in table schemas.
     * It generates 'CREATE' and 'DELETE' scripts for the Hibernate entities.
     * Current implementation involves usage of {@link #write(FileOutputStream, String[], Formatter)} method.
     *
     * @param outputStream - stream will be used for *.sql file creation.
     * @throws IOException
     */
    public HibernateHelper translate(FileOutputStream outputStream) throws IOException {
        Dialect requiredDialect = Dialect.getDialect(config.getProperties());
        String[] query = null;

//        query = config.generateDropSchemaScript(requiredDialect);
//        write(outputStream, query, FormatStyle.DDL.getFormatter());

        query = config.generateSchemaCreationScript(requiredDialect);
        write(outputStream, query, FormatStyle.DDL.getFormatter());

        return this;
    }

    /**
     * Method writes line by line DDL scripts in the output stream.
     * Also each line logs in the console.
     *
     * @throws IOException
     */
    private void write(FileOutputStream outputStream, String[] lines, Formatter formatter)
            throws IOException {
        String tempStr = null;
        for (String line : lines) {
            tempStr = formatter.format(line) + ";";
            System.out.println(tempStr);
            outputStream.write(tempStr.getBytes());
        }
    }
}
